<?php

class BackupController extends Controller {

	/**
	 * 默认显示备份主页面
	 */
	public function index() {
		$dataDir = Dir::tree('Backup');
		$this -> assign('dataDir', $dataDir);
		$this -> display();
	}

	/**
	 * 数据备份操作
	 */
	public function backup() {
		$result = Backup::backup(array('size' => 200, 'dir' => 'Backup/' . date('ymdHis'), 'step_time' => 1));
		if ($result === false) {
			$this -> error(Back::$error, U('Index/index'));
		} else {
			if ($result['status'] == 'success') {
				$this -> success('备份成功', U('index'));
			} else {
				$this -> success($result['message'], $result['url'], 0.2);
			}
		}
	}

	/*
	 * 数据还原
	 */
	public function recovery() {
		//备份目录
		if (!isset($_SESSION['dir'])) {
			$_SESSION['dir'] = Q('post.dir');
		}
		$result = Backup::recovery(array('dir' => $_SESSION['dir']));
		//还原发生错误
		if ($result === false) {
			$this -> error(Backup::$error, U('index'));
		} else {
			//还原完毕
			if ($result['status'] == 'success') {
				unset($_SESSION['dir']);
				$this -> success($result['message'], U('index'));
			} else {
				//还原过程中
				$this -> success($result['message'], $result['url'], 0.2);
			}
		}
	}


}
